package util;

import android.content.Context;
import android.content.SharedPreferences;


import org.json.JSONObject;

import java.io.File;
import java.util.Map;
import java.util.Set;


public class CacheManager {
    private static String fileCacheIndex = "filecacheindex";

    public String getCacheSize(Context context) {
        String size = "0KB";
        try {
            long fileSize = 0;
            fileSize += FileUtils.getDirSize(context.getFilesDir());
            fileSize += FileUtils.getDirSize(context.getCacheDir());
            //fileSize += FileUtils.getDirSize(context.getExternalCacheDir());
            //fileSize += FileUtils.getDirSize(context.getObbDir());
            //todo
            //fileSize += FileUtils.getDirSize(AppContext.getAppContext().getImageLoader().getDiskCache().getDirectory());
            //fileSize += FileUtils.getDirSize(new File("/data/data/"+context.getPackageName()+"/shared_prefs"));
            if (fileSize > 0) {
                size = FileUtils.formatFileSize(fileSize);
            }
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return size;
        }
    }

    public void clearCache(Context context) {
        try {
//            AppContext.getAppContext().getImageLoader().clearDiskCache();
//            AppContext.getAppContext().getImageLoader().clearMemoryCache();
            clearCacheFolder(context.getCacheDir(), System.currentTimeMillis());
            clearCacheFolder(context.getExternalCacheDir(), System.currentTimeMillis());
            clearCacheFolder(context.getObbDir(), System.currentTimeMillis());
            clearCacheFolder(context.getFilesDir(), System.currentTimeMillis());

            SharedPreferences preferences = context.getSharedPreferences(fileCacheIndex, Context.MODE_PRIVATE);
            Map<String, ?> map = preferences.getAll();
            if (!map.isEmpty()) {
                Set<String> key = map.keySet();
                for (String s : key) {
                    File file = new File("/data/data/" + context.getPackageName() + "/shared_prefs/" + s + ".xml");
                    file.delete();
                }
            }
            File file = new File("/data/data/" + context.getPackageName() + "/shared_prefs/" + fileCacheIndex + ".xml");
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private int clearCacheFolder(File dir, long curTime) {
        int deletedFiles = 0;
        if (dir != null && dir.isDirectory()) {
            try {
                for (File child : dir.listFiles()) {
                    if (child.isDirectory()) {
                        deletedFiles += clearCacheFolder(child, curTime);
                    }
                    if (child.lastModified() < curTime) {
                        if (child.delete()) {
                            deletedFiles++;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return deletedFiles;
    }

    public static void saveDataCache(String key, JSONObject dataJson, Context context) {
        if (key == null || key.isEmpty() || dataJson == null) {
            return;
        }
        String md5Str = MD5Utils.getStringDigest(key);

        SharedPreferences prefs = context.getSharedPreferences(md5Str, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(md5Str, dataJson.toString());
        editor.commit();
        SharedPreferences preferences = context.getSharedPreferences(fileCacheIndex, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor1 = preferences.edit();
        editor1.putString(md5Str, md5Str);
        editor1.commit();

    }

    public static JSONObject getDataCache(String key, Context context) {
        if (key == null || key.isEmpty()) {
            return null;
        }
        String md5Key = MD5Utils.getStringDigest(key);
        SharedPreferences prefs = context.getSharedPreferences(md5Key, Context.MODE_PRIVATE);
        String strData = prefs.getString(md5Key, null);
        if (strData == null) {
            return null;
        } else {
            try {
                return new JSONObject(strData);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}
